package com.xqy.service.impl;

import com.sun.javaws.exceptions.ExitException;
import com.xqy.entity.UserEntity;
import com.xqy.modle.User;
import com.xqy.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * description: UserServiceImpl
 * date: 2020/9/22 15:18
 * author: ASUS
 * version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
        /**
         * description: 查询
         * version: 1.0 
         * date: 2020/9/22 16:21
         * author: ZhouLiHao
         * 
         * @return java.util.List<com.xqy.entity.UserEntity>
         */
    @Override
    public List<UserEntity> selectUserShow() {
        List<UserEntity> entities=new ArrayList<>();
        List<User> users=User.dao.find("select * from user");
        for(User user:users){
           UserEntity userEntity=new UserEntity();
           userEntity.setName(user.getStr("name"));
           userEntity.setId(user.getInt("id"));
           userEntity.setAge(user.getInt("age"));
           entities.add(userEntity);
            System.out.println(user.getStr("name"));//查看有没有走数据库
        }
        return entities;
    }
    /**
     * description: 新增 
     * version: 1.0 
     * date: 2020/9/22 16:17 
     * author: ZhouLiHao
     * 
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addUser(UserEntity entity) {
        boolean yn=new User().set("name", entity.getName()).set("age", entity.getAge()).save();
        if(yn){
            System.out.println("新增成功");
        }else{
            System.out.println("新增失败");
        }
        return yn;
    }
    /**
     * description: 修改
     * version: 1.0
     * date: 2020/9/22 16:17
     * author: ZhouLiHao
     *
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateUser(UserEntity entity) {
            boolean yn = User.dao.findById(entity.getId()).set("name", entity.getName()).set("age",entity.getAge()).update();
            if (yn) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        return  yn;
    }

    /**
     * description: 删除 
     * version: 1.0 
     * date: 2020/9/22 16:17 
     * author: ZhouLiHao
     * 
     * @return void
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteUser(int[] ids) {
        boolean yn = false;
      try {
        for(int id:ids){
         yn=User.dao.deleteById(id);
        }
        }catch (Exception e){
         System.out.println("删除失败");
        throw  e;
            }
            return yn;
    }
    /**
     * description: 根据id查询单个用户 
     * version: 1.0 
     * date: 2020/9/22 17:16 
     * author: ZhouLiHao
     * 
     * @return com.xqy.entity.UserEntity
     */ 
    @Override
    public UserEntity findone(int id) {
        User user=User.dao.findById(id);
        UserEntity entity=new UserEntity();
        entity.setId(user.getInt("id"));
        entity.setName(user.getStr("name"));
        entity.setAge(user.getInt("age"));
        return entity;
    }
}
